#ifndef _MULTICASTCONNECTOR_H_
#define _MULTICASTCONNECTOR_H_

#include <Poco/Net/MulticastSocket.h>
using Poco::Net::MulticastSocket;

#include <Poco/Runnable.h>
using Poco::Runnable;

#include <Poco/Net/SocketAddress.h>
using Poco::Net::SocketAddress;

#include <Poco/Types.h>
using Poco::UInt16;
using Poco::UInt64;

#include <Poco/Net/IPAddress.h>
using Poco::Net::IPAddress;

#include <string>
using std::string;

#include "command/commandqueue.h"

#include <Poco/Timer.h>
using Poco::Timer;
using Poco::TimerCallback;


/**
 * Provides multicast functionality. Has to be integrated into a Node.
 */
class MulticastConnector : public Runnable
{
	public:
		/*
		 * Default Constructor
		 */
		MulticastConnector();

		/*
		 * Initializes a socket with a certain address.
		 */
		MulticastConnector(SocketAddress groupport, CommandQueue *commands);

		/*
		 * Initializes a multicastsocket with a certain group and port.
		 */
		MulticastConnector(IPAddress group,  UInt16 port, CommandQueue *commands);

		virtual ~MulticastConnector();

		/**
		 * Starts the multicastthread
		 */
		void run();

		/**
		 * Stops the multicastthread.
		 */
		void stop();

		/**
		 * Sends a command on the ring
		 */
		void sendCommand(string const& repr);

		/**
		 * Returns the address of the socket.
		 */
		string getAddress();

		const string requestRingInfo();
		void sendRingInfo();
	private:

		void onTimer(Timer &timer);

		/**
		 * The actual mcast socket.
		 */
		MulticastSocket _mcast;

		/*
		 * The group that this socket belongs to.
		 */
		SocketAddress _group;

		/*
		 * Queue that contains the commands that need to be executed.
		 */
		CommandQueue* cmds;
};

#endif /* _MULTICASTCONNECTOR_H_ */
